﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Runtime.InteropServices;

namespace Web.PageUI
{
    /// <summary>
    /// 模板基类
    /// 所有的全局方法写在此类下面即可
    /// 不包含后台页面
    /// </summary>
    public class Base : Evan.WebPageUI.Base
    {

        public List<dynamic> footChannelList = new List<dynamic>();

        public Base()
        {
            footChannelList = allChannelList.FindAll(res=>res.FootNav==1);
        }
        /// <summary>  
        /// 重写Render方法，切换语言
        /// </summary>  
        /// <param name="writer"></param>  
        protected override void Render(HtmlTextWriter writer)
        {
            StringWriter sw = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
            base.Render(htmlWriter);
            string html = sw.ToString();
            if (Request.Headers["language"] != null)
            {
                string language = Request.Headers["language"] as string;
                html = html.Replace("href=\"/", "href=\"/" + language + "/");
                html = html.Replace("src=\"/", "src=\"/" + language + "/");
                html = html.Replace("url(\"/", "url(\"/" + language + "/");
                html = html.Replace("url('/", "url('/" + language + "/");
                html = html.Replace("url(/", "url(/" + language + "/");

                html = html.Replace("\"/ashx/", "\"/" + language + "/ashx/");
                //html = html.Replace("data-src=\"/", "data-src=\"/" + language + "/");
                html = html.Replace("{none}", "");
            }
            html = ToTraditional(html, LCMAP_SIMPLIFIED_CHINESE);   //繁体转简体
            html = html.Replace("於","于");
            html = html.Replace("於", "于");
            //if (Evan.Common.CommFun.GetAppSetting("change") != "0")
            //{
            //    if (Evan.Common.CommFun.GetSession("language") == "simple")
            //    {
            //        html = ToTraditional(html, LCMAP_SIMPLIFIED_CHINESE);   //繁体转简体
            //    }
            //    //html = ToTraditional(html, LCMAP_TRADITIONAL_CHINESE);   //简体转繁体
            //}
            writer.Write(html);
        }


        [DllImport("kernel32.dll", EntryPoint = "LCMapStringA")]
        public static extern int LCMapString(int Locale, int dwMapFlags, byte[] lpSrcStr, int cchSrc, byte[] lpDestStr, int cchDest);
        const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
        const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;

        //转化方法
        public static string ToTraditional(string source, int type)
        {
            byte[] srcByte2 = Encoding.Default.GetBytes(source);
            byte[] desByte2 = new byte[srcByte2.Length];
            LCMapString(2052, type, srcByte2, -1, desByte2, srcByte2.Length);
            string des2 = Encoding.Default.GetString(desByte2);
            return des2;
        }



        /// <summary>

        /// 获取当前栏目指定层级的栏目

        /// </summary>

        /// <param name="level"></param>

        /// 从下往上

        /// <returns></returns>

        public dynamic GetChannelByLevel(int level = 1)

        {

            List<int> levelIdList = GetIdArry(curChannel);

            levelIdList.Reverse();

            if (levelIdList.Count < level)

            {

                return null;

            }

            else

            {

                return allChannelList.Find(res => res.Id == levelIdList[level - 1]);

            }

        }

        public List<int> GetIdArry(dynamic channel)

        {

            List<int> channelIdlist = new List<int>();

            if (channel != null)

            {

                channelIdlist.Add(channel.Id);

                dynamic ChannelList = allChannelList.Find(res => res.Id == channel.ParentId);

                channelIdlist.AddRange(GetIdArry(ChannelList));

            }

            return channelIdlist;

        }



    }
}
